#include "WebSocketCaster.h"
#include "CHttpServer.h"
#include "Path.h"

WebSocketCaster::WebSocketCaster():
	m_server(new CHttpServer())
{
	std::string workdir = comn::Path::getWorkDir();

	m_server->setWebRoot(workdir.c_str());
	m_server->setUserFile(NULL);

}

WebSocketCaster::~WebSocketCaster()
{
	stop();
}

bool WebSocketCaster::start(int port)
{
	return m_server->start(port, this);
}

void WebSocketCaster::stop()
{
	m_server->stop();
}

bool WebSocketCaster::isStarted()
{
	return m_server->isStarted();
}

void WebSocketCaster::sendBinary(void* data, size_t size)
{
	m_server->broadcastBinary(data, size);
}

void WebSocketCaster::sendText(const std::string& text)
{
	m_server->broadcast(const_cast<char*>(text.c_str()), text.size());
}

int WebSocketCaster::handleJsonApi(HttpRequest& request, std::string& json)
{
	return EINVAL;
}

void WebSocketCaster::handleUpload(const char* name, const std::string& filepath)
{
	// pass
}

int WebSocketCaster::handleFileApi(HttpRequest& request, std::string& resp, std::string& mime)
{
	return 0;
}

void WebSocketCaster::handleWebSocketFrame(unsigned char *data, size_t size, unsigned char flags)
{
	return;
}

